package selfview.sufutian.com.myapplication.draw2d;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;


/**
 * Created by sufutian on 2018/10/11.
 * 可扩散TextView--部分变颜色
 * 外层嵌套ScrollView可实现自动滚动字幕--仿讯飞听写页面
 */

public class SubtitleTextView extends AppCompatTextView {

    private int mLineY;
    private int mViewWidth;
    private TextPaint showPaint;

    private int drawPosition;

    public SubtitleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SubtitleTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public SubtitleTextView(Context context) {
        super(context);


    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    private int startPosition;
    private int endPosition;

    /**
     * 刷新字符颜色
     *
     * @param startPosition 开始位置
     * @param endPosition   结束位置
     */
    public void refreshShowContent(int startPosition, int endPosition) {
        this.startPosition = startPosition;
        this.endPosition = endPosition;
        drawPosition = 0;
        postInvalidate();
    }

    private int currentY;

    /**
     * 变色位置的y值
     *
     * @return
     */
    public int getCurrentY() {
        return currentY;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        TextPaint paint = getPaint();
        paint.setColor(getCurrentTextColor());
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.drawableState = getDrawableState();
        if (showPaint == null) {
            showPaint = new TextPaint();
            showPaint.setTextSize(paint.getTextSize());
            showPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
            showPaint.setColor(Color.BLUE);
        }
        mViewWidth = getMeasuredWidth();
        String text = (String) getText().toString();
        mLineY = 0;
        mLineY += getTextSize() * 1.0;
        Layout layout = getLayout();
        for (int i = 0; i < layout.getLineCount(); i++) {
            int lineStart = layout.getLineStart(i);//行首
            int lineEnd = layout.getLineEnd(i);//行尾
            String line = text.substring(lineStart, lineEnd);//行
            float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, getPaint());//该行实际宽度
            if (i == layout.getLineCount() - 1) {//最后一行
                drawCharText(canvas, lineStart, line, width, true);
            } else {
                drawCharText(canvas, lineStart, line, width, false);
            }
            mLineY += getLineHeight();//text 的y位置
        }
    }

    private void drawCharText(Canvas canvas, int lineStart, String line, float lineWidth, boolean lastLine) {
        float x = 0;
        float d = (mViewWidth - lineWidth) / line.length() - 1;//字间距
        if (lastLine) {
            d = 0;
        }
        for (int i = 0; i < line.length(); i++) {
            String c = String.valueOf(line.charAt(i));//字符
            float cw = StaticLayout.getDesiredWidth(c, getPaint());//字符宽度
            if (drawPosition >= startPosition && drawPosition <= endPosition) {
                canvas.drawText(c, x, mLineY, showPaint);
                currentY = mLineY;
            } else {
                canvas.drawText(c, x, mLineY, getPaint());
            }
            drawPosition++;//记录字符长度
            x += cw + d;//x=字符宽度+字间距
        }
    }


}